<?php
namespace app\index\controller;
use app\index\model\Wxapp as WxappModel;
use app\index\model\StoreUser as StoreUserModel;
use app\index\model\WebOrder as WebOrderModel;
use app\index\model\WebSet as WebSetModel;
use app\index\model\WebEdition as WebEditionModel;
use app\common\library\wechat\WxPay;
/**
 * 用户首页
 */
class User extends Controller
{
	/**
     * 获取列表
     */
    public function index()
    {
		$model = new WxappModel;
        $list = $model->getList($this->store_user_id);
		$this->assign('title','商户中心');
		$this->assign('key', 'user');
        return $this->fetch('index', compact('list'));
    }

	
	/**
     * 商户钱包
     */
    public function wallet()
    {
		$store = StoreUserModel::detail($this->store['user']['store_user_id']);
        if (!$this->request->isAjax()) {
            $this->assign('title','账户钱包');
			$this->assign('key', 'wallet');
			$this->assign('wallet',$store['wallet']);
			return $this->fetch('wallet');
        }
		$model = new WebOrderModel;
		$pay = $this->postData('pay');
		// 创建订单
		if($model->add($this->store['user']['store_user_id'],$pay['money'])) {
			return $this->renderSuccess('二维码生成中...', url('user/pay',['order_no'=>$model['order_no'],'money'=>$pay['money']]));
        }
        $error = $model->getError() ?: '订单创建失败';
        return $this->renderError($error);
    }
	
	/**
     * 商户充值
     */
    public function pay($order_no,$money)
    {
		$values = WebSetModel::getItem('pay');
		$WxPay = new WxPay($values);
		$code_url = $WxPay->unifiedorder($order_no, '', $money, 'native');
		$this->assign('title','充值二维码');
		$this->assign('key','pay');
		$this->assign('money',$money);
		$this->assign('code_url',$code_url);
		return $this->fetch('pay');
    }
	
	/**
     * 升级续费列表
     */
    public function upgrade($wxapp_id)
    {
		$wxapp = WxappModel::getWxapp(['wxapp_id' => $wxapp_id]);
		$edition = WebEditionModel::getAppType($wxapp['app_type']['value']);
		if (!$this->request->isAjax()) {
            $this->assign('title','小程序续费');
			$this->assign('key', 'upgrade');
			$this->assign('edition', $edition);
			$this->assign('wxapp', $wxapp);
			return $this->fetch('upgrade');
        }
		$model = new WebOrderModel;
		$data = $this->postData('wxapp');
		$data['store_user_id'] = $this->store['user']['store_user_id'];
		$data['shop_mode'] = $wxapp['shop_mode']['value'];
		$data['web_edition_id'] = $edition['web_edition_id'];
		$data['wxapp_id'] = $wxapp_id;
		// 创建订单
		if($model->upgrade($data)) {
			return $this->renderSuccess('续费成功', url('user/index'));
        }
        $error = $model->getError() ?: '续费失败';
        return $this->renderError($error);
    }
	
	/**
     * 添加
     */
    public function addWxapp()
    {
        if (!$this->request->isAjax()) {
			$list = WebEditionModel::getALL();
            $this->assign('title','创建微信小程序');
			$this->assign('key', 'user');
            return $this->fetch('addwxapp',compact('list'));
        }
		if($this->store['user']['user_name']=='admin'){
			return $this->renderError('测试账号不可创建小程序');
		}
        $model = new WxappModel;
		$wxapp = $this->postData('wxapp');
		$wxapp['store_user_id'] = $this->store['user']['store_user_id'];
        if ($model->add($wxapp)) {
            return $this->renderSuccess('创建成功', url('user/index'));
        }
        $error = $model->getError() ?: '创建失败';
        return $this->renderError($error);
    }

    /**
     * 删除
     */
    public function delete($wxapp_id)
    {
		return $this->renderError('暂不支持');
		/*
        $model = GoodsModel::get($goods_id);
        if (!$model->remove()) {
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess('删除成功');
		*/
    }

    /**
     * 登录小程序
     */
    public function wxappLogin($wxapp_id)
    {
        // 小程序详情
		if($model = WxappModel::getWxapp(['wxapp_id' => $wxapp_id])){
			if ($model->wxappLogin($model)) {
				$this->redirect('/store.php');
				return false;
			}
		}
		$this->error('应用错误', '/user.php');
		return false;		
    }
	
	/**
     * 修改管理员密码
     */
    public function renew()
    {
        $model = StoreUserModel::detail($this->store['user']['store_user_id']);
        if ($this->request->isAjax()) {
			if($this->store['user']['user_name']=='admin'){
				return $this->renderError('测试账号不可修改密码');
			}
            if ($model->renew($this->postData('user'))) {
                return $this->renderSuccess('修改成功','/user.php');
            }
            return $this->renderError($model->getError() ?: '修改失败');
        }
		$this->assign('title','修改密码');
		$this->assign('key', 'user');
        return $this->fetch('renew', compact('model'));
    }
}
